// JavaScript Document
function Ajax(){
    var _self=this;
    var async=true;
    var xmlHttp=null;
    this.errorFunction=null;
    this.completeFunction=null;
    this.successFunction=null;
    
    try{
        xmlHttp=new XMLHttpRequest();
    }
    catch (e){
        try{
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e){
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    xmlHttp.onreadystatechange=function(){
	    if(xmlHttp.readyState==4){
		    if(xmlHttp.status==200){
		        if(typeof(_self.successFunction)=="function"){
		            successFunction(xmlHttp);
		        }
		    }
		    else{
		        if(typeof(_self.errorFunction)=="function"){
		            _self.errorFunction(xmlHttp);
		        }
		    }
		    if(typeof(_self.completeFunction)=="function"){
		        _self.completeFunction(xmlHttp);
		    }
        }
    }
    
    this.setHeader=function(key,value){
        xmlHttp.setRequestHeader(key,value);
    }
    this.setAsync=function(bool){
        async=bool;
    }
    this.post=function(url,data){
        data=serialData(data);
        xmlHttp.open("POST",encodeURI(url),async);
        xmlHttp.send(data);
    }
    this.get=function(url,data){
        data=serialData(data);
        if(url.indexOf('?')!=-1){
            url+="&" + data;
        }
        else{
            url+="?" + data;
        }
        xmlHttp.open("GET",encodeURI(url),async); 
        xmlHttp.send();
    }
    function serialData(jsonObj){
        var serial=""; 
        for (var i in jsonObj ) {   
            if (typeof (i) == "function") continue; 
            if(serial!="") serial+="&";  
            serial += i + "=" + jsonObj [i].toString();   
        }   
        return serial;  
    }    
}